//
//  ViewControllerOfClickGuoWai.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewControllerOfClickGuoWai.h"
#import "CellForClickGuoWai.h"
#import "PCH.pch"
#import "ModelForClickGuoWai.h"
#import "VCForDestination_contents.h"
#import <UIImageView+WebCache.h>


@interface ViewControllerOfClickGuoWai () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)UIButton *buttonOfRight;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation ViewControllerOfClickGuoWai

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_tableView release];
    [_arrayOfModel release];
    [_buttonOfRight release];
    [_buttonOfLeft release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *title = [NSString stringWithFormat:@"%@攻略",self.name];
    self.navigationItem.title = title;
    
    self.tabBarController.tabBar.hidden = YES;

    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    /** 解析数据 */
    [self handleData];
    
    /** 创建tableView */
    [self createTableView];
}

#pragma mark - 解析数据
- (void)handleData {
    
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json?page=1", self.id];
    
    [NetworkingHandler networkingWithURL:str completeHandle:^(NSData *data,id result) {
        
        for (NSDictionary *dic in result) {
            ModelForClickGuoWai *model = [[ModelForClickGuoWai alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayOfModel addObject:model];
            [model release];
            
        }
        
        [self.tableView reloadData];
        
    }];
    
}

#pragma mark - 创建tableView
- (void)createTableView {
        
    self.arrayOfModel = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    label.textAlignment = 1;
    label.text = @"-----无更多攻略-----";
    self.tableView.tableFooterView = label;
    label.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);

    
    [self.tableView registerClass:[CellForClickGuoWai class] forCellReuseIdentifier:@"pool"];
 
}

/** 返回cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 220;
}

/** 返回cell个数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayOfModel.count;
}

/** 返回cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForClickGuoWai *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    [cell passModel:[self.arrayOfModel objectAtIndex:indexPath.row]];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    return cell;
}

/** 点击cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
   ModelForClickGuoWai *model = [self.arrayOfModel objectAtIndex:indexPath.row];
    NSMutableArray *array = model.destination_contents;
    
    if (array.count == 0) {
        
        return;
        
    } else {
        
        VCForDestination_contents *vc = [[VCForDestination_contents alloc] init];
        
        vc.id = model.id;
        vc.name_zh_cn = model.name_zh_cn;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
      
    }
}

#pragma mark - 左上角返回按钮
- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
